package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.text.Editable;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.weixin.paleituo.R;
import com.weixin.paleituo.base.BaseActivity;
import com.weixin.paleituo.help.TextWatcherImp;
import com.weixin.paleituo.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 搜索
 */

public class SearchGoodsActivity extends BaseActivity {

    @BindView(R.id.et_activity_search_goods)
    EditText mEtActivitySearchGoods;
    @BindView(R.id.tv_search_activity_search)
    TextView mTvSearchActivitySearch;
    @BindView(R.id.rl_search_root_activity_search_goods)
    RelativeLayout mRlSearchRootActivitySearchGoods;
/*    @BindView(R.id.flowlayout_activity_search_goods)
    FlowLayout mFlowlayoutActivitySearchGoods;*/
/*    @BindArray(R.array.hotSearchList)
   String[] mHotSearchList;*/

    @Override
    protected void initView() {
        setContentView(R.layout.activity_search_goods);
    }

    @Override
    protected void onStart() {
        super.onStart();
        mEtActivitySearchGoods.setText("");
    }

    @Override
    protected void initData() {
        super.initData();
      /*  for (String s:mHotSearchList){
             TextView textView= (TextView) UIUtils.inflate(this,R.layout.hot_search_item);
          //  textView.setText(s);
            textView.setText(s);
           mFlowlayoutActivitySearchGoods.addView(textView);

        }*/
        mEtActivitySearchGoods.setText("");

        mEtActivitySearchGoods.addTextChangedListener(new TextWatcherImp() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                String editable = mEtActivitySearchGoods.getText().toString();
                String regEx = "[^a-zA-Z0-9\\u4E00-\\u9FA5]";
                Pattern p = Pattern.compile(regEx);
                Matcher m = p.matcher(editable);
                String str = m.replaceAll("").trim();
                if (!editable.equals(str)) {
                    mEtActivitySearchGoods.setText(str);
                    mEtActivitySearchGoods.setSelection(str.length());
                }

                String editable1 = mEtActivitySearchGoods.getText().toString();

                if (StringUtils.isEmpty(editable1)) {
                    mTvSearchActivitySearch.setText("取消");
                } else {
                    mTvSearchActivitySearch.setText("搜索");
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }

        });

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);

        mEtActivitySearchGoods.setText("");

    }

    @OnClick(R.id.tv_search_activity_search)
    public void onMTvSearchActivitySearchClicked() {
        String flag = mTvSearchActivitySearch.getText().toString();
        if (flag.equals("取消")){
            //取消
            finish();
        }else {
            //去搜索
            ClassifyGoodsListActivity.startActivity(this,"",mEtActivitySearchGoods.getText().toString());

        }


    }

    public static void  startActivity(Context context){
        Intent intent=new Intent(context,SearchGoodsActivity.class);
        context.startActivity(intent);


    }
}
